<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Nov 20, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Web\SessionState
{
	use KLF\KCore;
	use KLF\System\KComponent;
	use KLF\System\Errors\KArgumentException;
	
	/**
	 *  Class KSessionHandler
	 *
	 *  Abstract class inherited by all session state handlers.
	 *
	 *  @package KLF\System\Web\SessionState
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *
	 *  @method KSessionHandler NewInstance
	 */
	abstract class KSessionStateBase extends KComponent
	{
		/**
		 * Base constructor for SessionState object(s)
		 */
		public function __construct()
		{
			parent::__construct();
			@session_set_save_handler(
				array($this, 'SessionOpen'),
				array($this, 'SessionClose'),
				array($this, 'SessionRead'),
				array($this, 'SessionWrite'),
				array($this, 'SessionDestroy'),
				array($this, 'SessionCleanup'));
		}
		
		/**
		 * A list of installed session handler types.
		 * 
		 * @var array $_rpropSessionHandlers
		 */
		protected static $_rpropSessionHandlers = array(
			'Custom' => array(
					'CustomProvider'	=> '',
					'IDManager'			=> '',
					'Cookieless'		=> false,
					'Timeout'			=> '15m'),
					
			'InProc' => array(
					'CustomProvider'	=> '',
					'IDManager'			=> '',
					'Cookieless'		=> false,
					'Timeout'			=> '15m'),
		
			'Off' => array(
					'CustomProvider'	=> '',
					'IDManager'			=> '',
					'Cookieless'		=> false,
					'Timeout'			=> '15m'),
				
			'SQLServer' => array(
					'CustomProvider'	=> '',
					'IDManager'			=> '',
					'Cookieless'		=> false,
					'Timeout'			=> '15m'),
					
			'StateServer' => array(
					'CustomProvider'	=> '',
					'IDManager'			=> '',
					'Cookieless'		=> false,
					'Timeout'			=> '15m')
		);
		
		/**
		 * 
		 * @param KSessionStateBase $config
		 */
		public static function CreateHandler($config)
		{
			
		}
		
		/**
		 * 
		 * Enter description here ...
		 * @param array $config
		 * @throws KArgumentException
		 */
		public static function GetHandler($config)
		{
			if (!is_array($config) || !isset($config['Mode']))
			{
				throw new KArgumentException("KHttpSession::GetHandler() expects array argument with 'Mode' element.");
			}
			$mode = $config['Mode'];
			if (isset(self::$_rpropSessionHandlers[$mode]))
			{
				$data = self::$_rpropSessionHandlers[$mode];
				foreach($config as $key => $val) {
					if (strlen($val) > 0) {
						$data[$key] = $val;
					}
				}
				return $data;
			}
			else
			{
				throw new KArgumentException(
					KCore::FS("GetHandler() does not support Mode '{mode}'", 
					array('mode' => $config['Mode'])));
			}
			return null;
		}
	}
}
